package com.hanxiaozhang.base.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.hanxiaozhang.base.KeyValuePair;
import com.hanxiaozhang.base.dto.FileDTO;
import com.hanxiaozhang.result.Result;
import com.hanxiaozhang.result.ResultCode;
import com.hanxiaozhang.util.PageUtil;
import com.hanxiaozhang.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanxiaozhang.base.entity.FileEntity;
import com.hanxiaozhang.base.service.FileService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 文件表
 *
 * @author hanxiaozhang
 * @email hanxiaozhang2018@sina.com
 * @date 2021-05-02
 */
@Slf4j
@Controller
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 查询列表
     *
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('base:file:list')")
    public Result<PageUtil> list(@RequestParam Map<String, Object> params) {

        Page<FileDTO> page = PageHelper
                .startPage(Integer.parseInt(params.get("current").toString()), Integer.parseInt(params.get("pageSize").toString()));
        List<FileDTO> fileList = fileService.list(params);
        return Result.success(new PageUtil(fileList, (int) page.getTotal()));
    }


    @PostMapping("/uploadFile")
    @ResponseBody
    @PreAuthorize("hasAuthority('base:file:uploadFile')")
    public Result uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("type") String type, @RequestParam("fileTag") String fileTag) {
        log.info("上传文件开始,type:[{}],fileTag:[{}]", type, fileTag);
        if (file == null || StringUtil.isBlank(file.getOriginalFilename())) {
            log.info("未选择文件");
            return Result.error(ResultCode.PARAM_IS_BLANK);
        }
        Map<String, Object> map = new HashMap<>(2);
        try {
            FileDTO fileDTO = fileService.uploadFile(file, type, fileTag);
            map.put("file", fileDTO);
            log.info("上传文件结束,type:[{}],fileTag:[{}],id:[{}],fileName:[{}]", type, fileTag, fileDTO.getFileId(), fileDTO.getFileFull());
        } catch (IOException e) {
            log.info("上传文件IO流异常：异常信息:[{}]", e.getMessage());
            return Result.error(ResultCode.IO_EXCEPTION);
        } catch (Exception e) {
            log.info("上传文件异常：异常信息:[{}]", e.getMessage());
            return Result.error(ResultCode.BUSINESS_EXCEPTION);
        }

        return Result.success(map);
    }


    /**
     * 修改
     *
     * @param file
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('base:file:update')")
    public Result<Void> update(FileEntity file) {

        fileService.update(file);
        return Result.success();
    }


    /**
     * 删除
     *
     * @param fileId
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    @PreAuthorize("hasAuthority('base:file:remove')")
    public Result<Void> remove(Long fileId) {

        fileService.remove(fileId);
        return Result.success();
    }


    /**
     * 批量删除
     *
     * @param fileIds
     * @return
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @PreAuthorize("hasAuthority('base:file:batchRemove')")
    public Result<Void> remove(Long[] fileIds) {

        fileService.batchRemove(fileIds);
        return Result.success();
    }


    /**
     * 批量更新属性
     *
     * @param fileIdAndBelongIdList
     * @return
     */
    @PostMapping("/batchUpdateBelongIdByFileId")
    @ResponseBody
    @PreAuthorize("hasAuthority('base:file:uploadFile')")
    public Result<Void> batchUpdateBelongIdByFileId(List<KeyValuePair<Long, Long>> fileIdAndBelongIdList) {

        fileService.batchUpdateBelongIdByFileId(fileIdAndBelongIdList);
        return Result.success();
    }

}
